//
//  leftViewController.m
//  忘不了
//
//  Created by rimi on 16/7/13.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "leftViewController.h"
#import "SDAutoLayout.h"
#import "UserModel.h"
#import "photoViewController.h"
#import "BaseRequest+MoneyAndRemember.h"
#import "UIImageView+WebCache.h"
#import "selfInfoViewController.h"
#import "UMSocial.h"
@interface leftViewController ()<UITableViewDataSource,UITableViewDelegate,UMSocialUIDelegate>
@property (nonatomic,strong)UIImageView *headView;
@property (nonatomic,strong)UIButton *button;
@property (nonatomic,strong)UIButton *quitButton;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)NSString *imageUrl;
@end

@implementation leftViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UserModel *user = [UserModel shareUserModel];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSDictionary *userInfo = [NSDictionary dictionary];
    userInfo = [userDefault objectForKey:@"userInfo"];
    if (user.isSan) {
        [_headView sd_setImageWithURL:[NSURL URLWithString:user.image]];
    }else{
        if (userInfo != nil) {
            [BaseRequest getHeadImageFromUserName:userInfo[@"username"] completionhanlder:^(Response *response) {
                if (response.isSuccess) {
                    NSLog(@"获取头像成功");
                    _imageUrl = response.resultObject;
                    NSLog(@"头像%@",response.resultObject);
                    [_headView sd_setImageWithURL:[NSURL URLWithString:_imageUrl]];
                }else{
                    NSLog(@"获取头像失败");
                    _headView.image = [UIImage imageNamed:@"head"];
                }
            }];
        }else{
            NSLog(@"获取头像失败");
            _headView.image = [UIImage imageNamed:@"head"];
        }
    }
    
    
    if ([UserModel shareUserModel].isLogin == YES) {
        _quitButton.hidden = NO;
        [self.button setTitle:[UserModel shareUserModel].userNmae forState:UIControlStateNormal];
    } else {
        _quitButton.hidden = YES;
        [self.button setTitle:@"未登录" forState:UIControlStateNormal];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.1647 green:0.1647 blue:0.2314 alpha:1.0];
    
    [self loadUI];
}

- (void)loadUI {
    _headView = [[UIImageView alloc]init];
    _headView.layer.cornerRadius = self.view.frame.size.width*0.3/2;
    _headView.layer.masksToBounds = YES;
    _headView.layer.borderWidth = 1;
    _headView.layer.borderColor = [UIColor grayColor].CGColor;
    _headView.userInteractionEnabled = YES;
    [_headView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapgesture:)]];
    [self.view addSubview:_headView];
    _headView.sd_layout
    .centerXIs(self.view.frame.size.width/4)
    .topSpaceToView(self.view,40)
    .widthIs(self.view.frame.size.width*0.3)
    .heightEqualToWidth();
    
    _button = [UIButton buttonWithType:UIButtonTypeSystem];
    [_button setTitle:@"未登录" forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor colorWithRed:0.8588 green:0.8314 blue:0.7725 alpha:1.0] forState:UIControlStateNormal];
    _button.backgroundColor = [UIColor clearColor];
    [_button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];
    _button.sd_layout
    .leftSpaceToView(self.view,0)
    .topSpaceToView(_headView,20)
    .widthIs(self.view.frame.size.width*0.5)
    .heightIs(self.view.frame.size.width*0.1);
    
    _quitButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_quitButton setTitle:@"退出" forState:UIControlStateNormal];
    [_quitButton setTitleColor:[UIColor colorWithRed:0.8588 green:0.8314 blue:0.7725 alpha:1.0] forState:UIControlStateNormal];
    _quitButton.backgroundColor = [UIColor clearColor];
    [_quitButton addTarget:self action:@selector(quitPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_quitButton];
    _quitButton.sd_layout
    .leftSpaceToView(self.view,0)
    .bottomSpaceToView(self.view,30)
    .widthIs(self.view.frame.size.width*0.5)
    .heightIs(self.view.frame.size.width*0.1);
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.width*0.3+110 ,self.view.frame.size.width*0.5 , self.view.frame.size.height-self.view.frame.size.width*0.3-200) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 50;
    [self.view addSubview:_tableView];
    
    _dataSource = [NSMutableArray array];
    _dataSource = @[@"个人信息",@"软件简介",@"常见问题",@"意见反馈",@"推荐给好友"].mutableCopy;
}

//头像点击手势响应事件
- (void)tapgesture:(UITapGestureRecognizer *)sender
{
    if ([UserModel shareUserModel].isLogin == NO) {
        UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您未登录，暂无法使用该功能" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (_delegate && [_delegate respondsToSelector:@selector(getText:)]){
                [_delegate getText:@"头像"];
            }
        }];
        [tips addAction:sure];
        [self presentViewController:tips animated:YES completion:nil];
    } else {
        photoViewController *photoVC = [[photoViewController alloc]init];
        photoVC.passValueHandle = ^(UIImage *image){
            //block 一旦被执行，就会执行此处的代码
            _headView.image = image;
        };
        photoVC.image = self.headView.image;
        [self presentViewController:photoVC animated:YES completion:nil];
    }
}

//未登录按钮点击响应事件
- (void)buttonPressed:(UIButton *)sender {
    if ([UserModel shareUserModel].isLogin == NO) {
        //3.调用协议方法
        if (_delegate && [_delegate respondsToSelector:@selector(getText:)]){
            [_delegate getText:sender.titleLabel.text];
        }
    } else {
        //3.调用协议方法
        if (_delegate && [_delegate respondsToSelector:@selector(getText:)]){
            [_delegate getText:@"个人信息"];
        }
    }
    
}

- (void)quitPressed:(UIButton *)sender {
    UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否退出登录？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        [UserModel shareUserModel].isLogin = NO;
        _quitButton.hidden = YES;
        [self.button setTitle:@"未登录" forState:UIControlStateNormal];
         _headView.image = [UIImage imageNamed:@"head"];
    }];
    UIAlertAction *canncel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [tips addAction:canncel];
    [tips addAction:sure];
   
    [self presentViewController:tips animated:YES completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifer = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor colorWithRed:0.8588 green:0.8314 blue:0.7725 alpha:1.0];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = _dataSource[indexPath.row];
    return cell;
}

//选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        if ([UserModel shareUserModel].isLogin == NO) {
            UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您未登录，暂无法查看" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [tips addAction:sure];
            [self presentViewController:tips animated:YES completion:nil];
        } else {
            //3.调用协议方法
            if (_delegate && [_delegate respondsToSelector:@selector(getText:)]){
                [_delegate getText:_dataSource[indexPath.row]];
            }
        }
    } else if (indexPath.row == 4) {
        [UMSocialData defaultData].extConfig.qqData.title = @"QQ分享title";
        [UMSocialData defaultData].extConfig.qzoneData.title = @"Qzone分享title";
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"579332bee0f55a9db70023f5"
                                          shareText:@"忘不了，忘不了，忘不了，http://baidu.com"
                                         shareImage:[UIImage imageNamed:@"个人"]
                                    shareToSnsNames:@[UMShareToQzone,UMShareToQQ]
                                           delegate:self];
    } else {
        //3.调用协议方法
        if (_delegate && [_delegate respondsToSelector:@selector(getText:)]){
            [_delegate getText:_dataSource[indexPath.row]];
        }
    }
}


@end
